<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 8/11/17
 * Time: 2:58 PM
 */

namespace api\modules\other\controllers;



use common\components\increment\Config;
use common\components\increment\Huirong;
use common\components\tools\Tools;
use common\models\workin\CarInvonceVerifyHuirong;
use common\models\workin\CarInvonceVerifyHuirongBack;
use common\models\workin\MortgageSignupHuirong;
use common\models\workin\MortgageSignupHuirongBack;
use wodrow\yii\rest\Controller;
//use yii\web\Controller;
use yii\base\ErrorException;
use yii\httpclient\Client;
use yii\log\FileTarget;

class HuiRongController extends Controller
{
    /**
     * 惠融 购车发票验证回调
     * @desc post
     * @param string $reportId
     * @param int $pocType
     * @param string $orderReportId
     */
    public function actionCarInvonceVerifyBack($reportId, $picType, $orderReportId)
    {
        // {"reportId":"20170810613QA8AE","picType":1111,"orderReportId":"vxdb014020170810215109098"}
        // ip：120.27.158.77
        $model = CarInvonceVerifyHuirong::findOne(['order_report_id'=>$orderReportId]);
        if (!$model){
            throw new ErrorException("没有找到对应订单", 1077);
        }
        $back_model = CarInvonceVerifyHuirongBack::findOne(['car_invonce_verify_huirong_id'=>$model->id]);
        $back_model = $back_model?$back_model:new CarInvonceVerifyHuirongBack();
        $back_model->car_invonce_verify_huirong_id = $model->id;
        $back_model->report_id = $reportId;
        $back_model->pic_type = $picType;
        $back_model->order_report_id = $orderReportId;
        $back_model->status = $back_model::STATUS_GET_BACK;
        if ($back_model->validate()&&$back_model->save()){
//            echo "保存数据成功1";
        }else{
            echo '{"code":-1, "msg":"error"}';exit;
        }
        $client = new Client();
        $data = [
            'grantKey' => $model->grant_key,
            'apiKey' => Config::$huirong['car_invoice_verify_back_result_api_key'],
            'reportId' => $back_model->report_id,
            'picType' => $back_model->pic_type,
        ];
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl(Huirong::getUrl())
            ->setData($data)
            ->send();
        $back_model->status = $back_model::STATUS_HAS_QUERY;
        $back_model->query_result = $response->content;
        if ($back_model->validate()&&$back_model->save()){
            echo '{"code":0, "msg":"success"}';exit;
        }else{
            echo '{"code":-1, "msg":"error"}';exit;
        }
    }

    /**
     * 抵押登记回调URL
     * @desc post
     * @param int $mortgageCode
     * @param string $reportId
     * @param string $plateNumber
     * @param string $mortgageMsg
     * @param string $orderReportId
     */
    public function actionMortgageSignupBack($mortgageCode = 1, $reportId, $plateNumber, $mortgageMsg = 'error', $orderReportId)
    {
        if (\Yii::$app->request->post('mortgageCode'))$mortgageCode = \Yii::$app->request->post('mortgageCode');
        if (\Yii::$app->request->post('busiCode'))$mortgageCode = \Yii::$app->request->post('busiCode');
        if (\Yii::$app->request->post('mortgageMsg'))$mortgageMsg = \Yii::$app->request->post('mortgageMsg');
        if (\Yii::$app->request->post('busiMsg'))$mortgageMsg = \Yii::$app->request->post('busiMsg');
        $model = MortgageSignupHuirong::findOne(['order_report_id'=>$orderReportId]);
        if (!$model){
            echo '{"code":-1, "msg":"error"}';exit;
//            throw new ErrorException("没有找到对应mortgage_signup", 1077);
        }
        $model->report_id = $reportId;
        $model->save();
        $back_model = MortgageSignupHuirongBack::findOne(['mortgage_signup_id'=>$model->id]);
        $back_model = $back_model?$back_model:new MortgageSignupHuirongBack();
        $back_model->mortgage_signup_id = $model->id;
        $back_model->report_id = $reportId;
        $back_model->plate_number = $plateNumber;
        $back_model->mortgage_code = $mortgageCode;
        $back_model->mortgage_msg = $mortgageMsg;
        if ($back_model->validate()&&$back_model->save()){
//            echo '{"code":0, "msg":"success"}';exit;
        }else{
            echo '{"code":-2, "msg":"error"}';exit;
//            throw new ErrorException("保存回调数据失败", 1078);
        }
        $client = new Client();
        $data = [
            'grantKey' => $model->grant_key,
            'apiKey' => Config::$huirong['mortgage_signup_back_result_api_key'],
            'reportId' => $back_model->report_id,
            'plateNumber' => $back_model->plate_number,
        ];
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl(Huirong::getUrl())
            ->setData($data)
            ->send();
        $back_model->status = $back_model::STATUS_HAS_QUERY;
        $back_model->query_result = $response->content;
        if ($back_model->validate()&&$back_model->save()){
            echo '{"code":0, "msg":"success"}';exit;
        }else{
            echo '{"code":-3, "msg":"error"}';exit;
        }
    }

    /**
     * 1.26网贷平台借贷查询（实时）
     * @desc 1.26网贷平台借贷查询（实时）
     * @param string $name 姓名
     * @param string $idCard 身份证
     */
    public function actionCreditInfo($name, $idCard)
    {
        $data = [
            'grantKey' => Config::$huirong['grantKey'],
            'apiKey' => Config::$huirong['credit_info_api_key'],
//            'businessCode' => '', // 业务落地行编码
//            'orderReportId' => '' // 请求方需携带一个本次请求的唯一标识
            'name' => '杨*', // 姓名
            'idCard' => '411327********2913', // 身份证号码
        ];
        $client = new Client();
//        var_dump(Huirong::getUrl());exit;
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl(Huirong::getUrl())
            ->setData($data)
            ->send();
//        var_dump($data);
        return json_decode($response->content);
    }
}